layui.use(['layer','upload','laydate','layedit','treeSelect','table','form','laytpl','element'], function(){
    var $ = layui.$
        ,layer = layui.layer
        ,admin = layui.admin
        ,table = layui.table
        ,element = layui.element
        ,form = layui.form
        ,laytpl = layui.laytpl;
    var delFlag = null;
    var treeSelect= layui.treeSelect;//树形下拉框
    var upload = layui.upload;
    var laydate = layui.laydate;
    window.layedit = layui.layedit;

    //获取路径参数
    var request = GetRequest();
    $("#produceBatchId").val(request.produceBatchId);
    $("#processId").val(request.processId);

    $.get("/record/check/" + request.id,function (data) {
        $("#id").val(data.id);
        $("#name").val(data.name);
        $("#produceBatchId").val(data.produceBatchId);
        $("#processId").val(data.processId);
        $("#number").val(data.number);
        $("#responsiblePerson").val(data.responsiblePerson);
        $("#remarks").val(data.remarks);

        laydate.render({
            elem: '#checkTime'
            ,value: new Date(data.checkTime) //必须遵循format参数设定的格式
        });

        //检查结果
        $("#layedit_content").html(data.checkResult);
        //创建一个编辑器
        window.editIndex = layedit.build('layedit_content',{
            height : 535,
            uploadImage : {
                url : "/layedit/upload/pic"
            }
        });
        layedit.setContent(editIndex,data.checkResult);
        //用于同步编辑器内容到textarea
        layedit.sync(editIndex);
        form.verify({
            content: function(value) {
                return layedit.sync(editIndex);
            }
        });
    });

    //修改投入品记录信息
    form.on('submit(edit)', function(data){
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        //初始化数据
        $.ajax({
            url: '/record/check/edit',
            data: data.field,
            type: 'POST',
            success: function (data) {
                if(data.code == 200) {
                    top.layer.close(index);
                    top.layer.msg("修改成功！");
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                } else {
                    top.layer.close(index);
                    top.layer.msg("修改失败！");
                }
            },
            error:function () {
                top.layer.close(index);
                top.layer.msg("修改失败！");
            }
        });
        return false;
    });

    /**
     * 获取路径中的参数
     * @returns {Object}
     */
    function GetRequest() {
        var url = location.search;         //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }

});

